/*
GET     /items              ->  index
GET     /items/new          ->  new
POST    /items              ->  create
GET     /items/:item       ->  show
GET     /items/:item/edit  ->  edit
PUT     /items/:item       ->  update
DELETE  /items/:item       ->  destroy
 */

var Db = require("./database");
db = new Db();

exports.index = function (req, res) {
	console.log('resource index');
	console.log(req.params);
	console.log(req.query);

	switch (req.params.format) {
        case (undefined):
        case ('html'):
            //res.render('devices', {});
            res.render(501); //Not Implemented
            break;
        case ('json'):
            console.log("get json devices");
            db.devices.find(req.query, function (error, results) {
                if (error) {
                    console.log(error);
                    res.send(500, error);
                } else {
                    console.log(results.length);
                    res.json(results);
                }
            });
            break;
        default:
            res.render(501); //Not Implemented
            break;
	}
};
exports.tree = function (req, res) {
    res.render('devices.tree.jade', {});
}
exports.status = function (req, res) {
    res.render(501);
}
exports.new = function (req, res) {
	console.log('new resource');
	console.log(req.params);
	res.render(501); //Not Implemented
};

exports.create = function (req, res) {
	console.log('create device');
	console.log(req.params);
	db.devices.create(req.body, function (error, ok) {
		if (error) {
            console.log(error);
            res.send(500, error);
        }
		else if (ok) {res.send(200);}
		else {res.send(500);}
	});
};

exports.show = function (req, res, next) {
	console.log('show device');
	console.log(req.params);
    //console.log(db.devices);
	switch (req.params.format) {
        case (undefined):
        case ('html'):
            res.render('devices.show.jade', {uuid: req.params.device});
            break;
        case ('json'):
            console.log("get json devices");
            db.devices.findOne({uuid: req.params.device}, function (error, results) {
                if (error) {
                    console.log(err);
                    res.json(err);
                } else if(results) {
                    var obj = results.toObject();
                    db.devices.events.find( {device: results.uuid}, function(error, events){
                        obj['events'] = events;
                        res.json(obj);
                    });
                } else {
                    console.log("NotFound");
                    res.send(404);
                    //next();
                }
            });
            break;
        default:
            res.render(501); //Not Implemented
            break;
	}
};
exports.events = function(req,res)
{
    console.log("Device events");
    switch (req.params.format) {
        case (undefined):
        case ('html'):
            res.render('devices.events.jade', {});
            break;
        case ('json'):
            db.devices.events.find( req.query, function(error, events){
                res.json(events);
            });
            break;
        default:
            res.render(501); //Not Implemented
            break;
	}
}
exports.newEventForOw = function(req,res)
{
    console.log("OW event");
    console.log(req.params);
    console.log(req.body);
    db.devices.findOne({protocol: 'ow', id: req.params.owid}, function(error, device){
        if( error ){
            console.log(error);
            res.send(500, error);
        } else if(device){
            var event = req.body;
            event['device'] = device.uuid;
            db.devices.events.create( event, function(error, event){
                if (error) {
                    console.log(error);
                    res.send(500, error);
                }
                else if (event) {res.json(event);}
                else {res.send(404);}
            });
        } else{
            res.send(404);
        }
    });
}
exports.newEvent = function(req,res)
{
    console.log("Device events");
    db.devices.findOne({uuid: req.params.device}, function(error, device){
        if( error ){
            console.log(error);
            res.send(500, error);
        } else if(device){
            var event = req.body;
            event['device'] = device.uuid;
            db.devices.events.create( event, function(error, event){
                if (error) {
                    console.log(error);
                    res.send(500, error);
                }
                else if (event) {res.json(event);}
                else {res.send(404);}
            });
        } else{
            res.send(404);
        }
    });
}
exports.edit = function (req, res) {
	console.log('edit resource ');
	console.log(req.params);
	res.render('devices.edit.jade', {uuid: req.params.device});
}

exports.update = function (req, res) {
	console.log('update device '+req.params.device);
	console.log(req.body);
    if( req.body.event )
    {
        var json = req.body.event;
        json['device'] = req.params.device;
        db.devices.events.create( json, function(error, ok){    
            if (error) {
                console.log(error);
                res.send(500, error);
            }
            else if (ok) {res.send(200);}
            else {res.send(500);}
        });
    } else
    {
        db.devices.update( {uuid: req.params.device}, req.body, function(error, ok){    
            if (error) {
                console.log(error);
                res.send(500, error);
            }
            else if (ok) {res.json(ok);}
            else {res.send(500);}
        });
    } 
};

exports.destroy = function (req, res) {
	console.log('destroy resource ');
	console.log(req.params);
	db.devices.remove( {uuid: req.params.device}, function(error, ok){    
        if (error) {
            console.log(error);
            res.send(500, error);
        }
		else if (ok) {res.send(200);}
		else {res.send(404);}
    });
};
